﻿<%@ Page Title="" Language="C#" MasterPageFile="~/administrator/backend.Master" AutoEventWireup="true" CodeBehind="user-list.aspx.cs" Inherits="VQWEB.administrator.membership.user_list" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Content_backend" runat="server">
    <form class="form-horizontal" method="post">
    <div id="search" class="form-group">
        <label class="col-md-1 control-label" for="keyword">
            Keyword:</label>
        <div class="col-md-4">
            <input id="keyword" class="form-control" placeholder="Keyword" type="text" />
        </div>
        <label class="col-md-1 control-label" for="select">
            Select:</label>
        <div class="col-md-2">
            <select name="role_id" class="form-control">
            </select>
        </div>
        <div class="col-md-2">
            <button class="btn btn-primary" type="submit">
                Search
            </button>
        </div>
    </div>
    <div class="table-responsive">
        <table class="table table-bordered table-hover">
            <thead>
                <tr class="active">
                    <th>
                        #
                    </th>
                    <th>
                        <input name="chkAll" type="checkbox" value=" " />
                    </th>
                    <th>
                        User Name
                    </th>
                    <th>
                        User Email
                    </th>
                    <th>
                        Roles Name
                    </th>
                    <th>
                        Modules
                    </th>
                    <th>
                        Actived
                    </th>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>
    </form>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Script_backend" runat="server">
    <script type="text/javascript">
        ShowToolBar("add");
        var url_ashx = "/admin/membership/handler";
        $("#btnAdd").live("click", function () {
            window.location = "/admin/membership/user/add";
        });

        $("#btnEdit").live("click", function () {
            var sId = $("input[name='chkItem']:checked").first().attr("value");
            if (sId != undefined)
                window.location = "/admin/membership/user/edit/" + sId;
        });

        $("#btnLockUnlock").live("click", function () {
            var array = new Array();
            $('input[name="chkItem"]:checked').each(function (i) {
                array[i] = this.value;
            });
            if (array.length > 0) {
                $.post(url_ashx, { lock_user: JSON.stringify(array) }, function (data) {
                    $("select[name='role_id']").empty();
                    $("tbody").empty();
                    var plusData = $.parseJSON(data);
                    ShowRolesHtml(plusData.roles);
                    ShowUsersHtml(plusData.users);
                });
            }
        });

        $("#btnDelete").live("click", function () {

            var array = new Array();
            $('input[name="chkItem"]:checked').each(function (i) {
                array[i] = this.value;
            });
            if (array.length > 0) {
                if (confirm("Ban chac chan xoa thanh vien ?")) {
                    $.post(url_ashx, { delete_users: JSON.stringify(array) }, function (data) {
                        $("select[name='role_id']").empty();
                        $("tbody").empty();
                        var plusData = $.parseJSON(data);
                        ShowRolesHtml(plusData.roles);
                        ShowUsersHtml(plusData.users);
                    });
                }
            }
        });

        $(document).ready(function () {
            $.get(url_ashx, { view_user: "view_user" }, function (data) {              
                var plusData = $.parseJSON(data);
                ShowRolesHtml(plusData.roles);
                ShowUsersHtml(plusData.users);
            });
        });

        function ShowRolesHtml(table) {
            $("select[name='role_id']").append("<option value=\"0\"> Lua chon vai tro </option>");
            $.each(table, function (i, obj) {
                $("select[name='role_id']").append("<option value=" + obj.role_id + ">" + obj.role_name + "</option>");
            });
        }

        function ShowUsersHtml(table) {
            var html = "";
            $.each(table, function (index, obj) {
                var user_id = obj.user_id;
                var user_name = obj.user_name;
                var user_email = obj.user_email;
                var user_active = (obj.user_active == true) ? "Active" : "DisActive";
                var role_name = (obj.role_name != null) ? obj.role_name : "";
                var role_module = obj.role_module;
                html += "<tr>";
                html += "<td>";
                html += index + 1;
                html += "</td>";
                html += "<td>";
                html += "<input type=\"checkbox\" name=\"chkItem\" value=" + user_id + " />";
                html += "</td>";
                html += "<td>";
                html += user_name;
                html += "</td>";
                html += "<td>";
                html += user_email;
                html += "</td>";
                html += "<td>";
                html += role_name;
                html += "</td>";
                html += "<td>";
                $.each(role_module, function (j, v) {
                    html += "[" + (j + 1) + "]" + v.module + "&nbsp;";
                });
                html += "</td>";
                html += "<td>";
                html += user_active;
                html += "</td>";
                html += "</tr>";
            });
            $("tbody").append(html);
        }
    </script>
</asp:Content>